﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_DSChuyenSinhHoatDoan : System.Web.UI.Page
{
    readonly SecurityLib.Sec_AES _se = new SecurityLib.Sec_AES();
    public string st = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindGrid();
            st = _se.Decrypt(Request.QueryString["tid"].Replace(" ", "+"), "gold", 128);
        }

    }
    
    //load du lieu vao gridview
    private void BindGrid()
    {
        _dbDataContext _db = new _dbDataContext();
        string donvi = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128); //MemberShip_ASP.GetMaDonViFromUserName(Page.User.Identity.Name);
        var val = from p in _db.tblChuyenCongTacDoans.Where(r=>r.DonViChuyenDi==donvi)             
                  select new
                  {
                    MaDoanVien=p.MaDoanVien,
                    TenDoanVien=p.tblDoanVien.HoTen,
                    Phai = (p.tblDoanVien.Phai == true ? "Nam" : "Nữ"),
                    NgaySinh = string.Format("{0:dd/MM/yyyy}", p.tblDoanVien.NgaySinh),
                    NgayChuyenDi = string.Format("{0:dd/MM/yyyy}", p.NgayChuyen),
                    DonViChuyenDi=p.tblCoSoDoan.TenDonVi,
                    DonViChuyenDen=(p.KhongCoTrongHeThong!="Đúng"?p.KhongCoTrongHeThong:p.tblCoSoDoan1.TenDonVi),

                  };
        Grid_DSDV_Xoa.DataSource = val;
        Grid_DSDV_Xoa.DataBind();
    }
    //xóa đoàn viên trưởng thành được chọn=checkbox
    
    public List<TextBox> GetTextBoxControls(Control ctrl)
    {
        var uCtrls = new List<TextBox>();
        foreach (Control child in ctrl.Controls)
        {
            if (child is TextBox) uCtrls.Add((TextBox)child);
            uCtrls.AddRange(GetTextBoxControls(child));
        }
        return uCtrls;
    }
    //xem chi tiết từng đoàn viên một=trực tiếp trên gridview
    protected void Grid_DSDV_Xoa_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        _dbDataContext _db = new _dbDataContext();
        string id = Grid_DSDV_Xoa.DataKeys[e.RowIndex].Value.ToString();
        pncontent.Visible = false;
        pndetail.Visible = true;
        //xoa toan bo du lieu cu truoc khi dua du lieu moi vao
        var listOfUserControls = GetTextBoxControls(pndetail);
        foreach (TextBox ctrl in listOfUserControls)
        {
            ctrl.Text = "";
            ctrl.ReadOnly = true;
        }
        //dua du lieu vao
        var list = _db.tblDoanViens.Single(r => r.MaDonVien.Trim() == id.Trim());
        txtMaDoanVien1.Text = list.MaDonVien.Trim();
        txtHoten1.Text = list.HoTen.Trim();
        txtgioitinh.Text = (list.Phai == true ? "Nam" : "Nữ");
        txtngaysinh.Text = string.Format("{0:dd/MM/yyyy}", list.NgaySinh);
        txtCMND.Text = list.CMND;
        txtngaycap.Text = string.Format("{0:dd/MM/yyyy}", list.NgayCapCMND);
        txtNoiCap.Text = list.NoiCap;
        txtNguyenquan.Text = list.NguyenQuan;
        txtThuongTru.Text = list.HoKhauThuongTru;
        txtHienNay.Text = list.ChoOHienNay;
        txtDienthoai.Text = list.DienThoai;
        txtEmail.Text = list.Email;
        try { txtdantoc.Text = list.tblDanhMuc.TenDanhMuc; }
        catch { }
        try { txttongiao.Text = list.tblDanhMuc1.TenDanhMuc; }
        catch { }
        try { txthonnhan.Text = list.tblDanhMuc2.TenDanhMuc; }
        catch { }
        txtXuatthan.Text = list.XuatThan;
        txtNghenghiep.Text = list.NgheNghiep;
        txtMota.Text = list.MoTaCongViec;
        txtDiaBanDanCu.Text = (list.DiaBanDanCuID != null ? list.tblDanhMuc5.TenDanhMuc : "");
        txttrinhdo.Text = list.TrinhDoVanHoa;
        txttrinhdochuyenmon.Text = (list.TrinhDoChuyenMonID != null ? list.tblDanhMuc7.TenDanhMuc : "");
        txtdangvien.Text = ((bool)list.DangVien == true ? "Có" : "Không");
        txtChucVuDoan.Text = list.tblDanhMuc6.TenDanhMuc.Trim();
        txtbang1.Text = list.BangCap.Split('@')[0];
        txtbang2.Text = list.BangCap.Split('@')[1];
        try { txtngoaingu.Text = list.tblDanhMuc3.TenDanhMuc; }
        catch { }
        try { txttinhoc.Text = list.tblDanhMuc4.TenDanhMuc; }
        catch { }
        txtsuckhoe.Text = list.SucKhoe;
        txtngayvaodoan.Text = string.Format("{0:dd/MM/yyyy}", list.NgayVaoDoan);
        if (list.PhatTrienMoi == false)
        {
            txtphattrienmoi.Text = (list.PhatTrienMoi == true ? "Phát triển mới" : "Đơn vị khác chuyển đến");
            txtngaychuyenden.Text = string.Format("{0:dd/MM/yyyy}", list.NgayChuyenDen);
            txtDonViCu.Text = list.DonViCu;
            txtphattrienmoi.Visible = true;
            txtngaychuyenden.Visible = true;
            txtDonViCu.Visible = true;
        }
        else
        {
            txtphattrienmoi.Visible = false;
            txtngaychuyenden.Visible = false;
            txtDonViCu.Visible = false;
        }
    }
    //chọn all checkbox
    protected void chkInGridviewHeader_CheckedChanged(object sender, EventArgs e)//day la ham de goi trong header cua itemtemplate
    {
        CheckBox chk = (CheckBox)sender;
        CheckBox chkitem;
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            chkitem = (CheckBox)dr.FindControl("chkchon");
            if (chk.Checked == true)
            {
                chkitem.Checked = true;
            }
            else
            {
                chkitem.Checked = false;
            }
        }
    }
    //Eport to Excel
    public void ExporttoExcel(object sender, EventArgs e)
    {
        Session.Remove("ListMaDoanVien_Print");
        Session.Remove("TenDonVi_Print");
        string listmadoanvien = "";
        CheckBox ckh;
        Label lbl;
        int count = 0;
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            ckh = (CheckBox)dr.FindControl("chkchon");
            if (ckh.Checked == true)
            {
                lbl = (Label)dr.FindControl("lblMaDoanVien");
                listmadoanvien += lbl.Text.Trim() + "!";
                count++;
            }
        }
        if (count > 0)
        {
            Session["ListMaDoanVien_Print"] = listmadoanvien;
            Session["TenDonVi_Print"] = _se.Decrypt(Request.QueryString["tid"].Replace(" ", "+"), "gold", 128);
            Response.Redirect("javascript:window.open('../Print/PrintPage.aspx','_blank');");
        }
        else
        {
            SecurityLib.ToolsMore.ShowPopUpMsg("Chưa chọn đoàn viên để in.", this.Page);
        }
    }
    //button dong panel chi tiet doan vien 
    protected void btnclose_Click(object sender, EventArgs e)
    {
        pncontent.Visible = true;
        pndetail.Visible = false;
    }
}